
coos.form.input = function(){};

coos.form.input.radio = function(name)
{
	var rs = coos.$name(name);
	var len = rs.length;
	for(var i = 0; i < len; i++)
	{
		if(rs[i].checked)
		{
			return rs[i];
		}
	}
	return null;
};

coos.form.checkbox = function(){};

coos.form.checkbox.getElements = function(formElement)
{
	var form = coos.$obj(formElement) || document;
    return form.getElementsByTagName("input"); 
};

coos.form.checkbox.isSelect = function(formElement) 
{
	var els = coos.form.checkbox.getElements(formElement);
    var len = els.length;
    for (var i = 0; i < len; i++)
    {
		if(els[i].checked == true)
		{
       		return true;
		}
    }
    return false;
};

coos.form.checkbox.selectAll = function(formElement) 
{
	var els = coos.form.checkbox.getElements(formElement);
    var len = els.length;
    for (var i = 0; i < len; i++)
    {
		if(els[i].type == "checkbox")
		{
       		els[i].checked = true;
		}
    }
};

coos.form.checkbox.unselectAll = function(formElement) 
{
	var els = coos.form.checkbox.getElements(formElement);
    var len = els.length;
    for (var i = 0; i < len; i++)
    {
		if(els[i].type == "checkbox")
		{
       		els[i].checked = false;
		}
    }
};

coos.form.checkbox.reverse = function(formElement) 
{
	var els = coos.form.checkbox.getElements(formElement);
    var len = els.length;
    for (var i = 0; i < len; i++)
    {
		if(els[i].type == "checkbox")
		{
			els[i].checked = !els[i].checked;
		}
    }
};

coos.form.checkbox.values = function(formElement) 
{
	var els = coos.form.checkbox.getElements(formElement);
    var len = els.length;
    var s = "";
    for (var i = 0; i < len; i++)
    {
		if(els[i].type == "checkbox")
		{
			if (els[i].checked)
			{
				s += els[i].name + "=" + els[i].value + "&";
			}
		}
    }
    return s;
};

coos.form.validate.htmltotext = function(html)
{
	return html.replace(/<[^>]*>/g,"");
};

coos.form.validate.limitlength = function(obj,length)
{
	if(obj.value.length > length)
	{
		obj.value = obj.value.substring(0,length);
		alert("长度不能超过" + length + "个字符！");
	}
};

/**
 * 验证单个表单项不能为空
 */
coos.form.validate.empty = function(formId,fieldName,message)
{
	var message = message || "内容不能为空，请填写后提交！";
	var form = coos.$id(formId);
	if(form[fieldName].value.trim() == "")
	{
		alert(message);
		form[fieldName].focus();
		return false;
	}
	return true;
};

/**
 * 批量验证表单项不能为空
 */
coos.form.validate.emptys = function(formId,fieldNames,messages)
{
	var message = "内容不能为空，请填写后提交！";
	var form = coos.$id(formId);
	for(var i=0;i<fieldNames.length; i++)
	{
		if(form[fieldNames[i]] && form[fieldNames[i]].value.trim() == "")
		{
			if(messages && messages[i])
			{
				alert(messages[i]);
			}
			else
			{
				alert(message);
			}
			form[fieldNames[i]].focus();
			return false;
		}
	}
	return true;
};

/**
 * 验证表单
 * coos.form.validate.regex("pageForm","clothStockIn.discount","^[0-9]+.?[0-9]*$","必须为数字");
 */
coos.form.validate.regex = function(formId,fieldName,regex,message)
{
	var message = message || "输入格式不对，请改正后提交！";
	var form = coos.$id(formId);
	var oReg = new RegExp(regex);
	var value = form[fieldName].value.trim();
	if(value != "" && !oReg.test(value))
	{
		alert(message);
		form[fieldName].focus();
		return false;
	}
	return true;
};

/**
 * 批量验证表单
 * coos.form.validate.regexs("pageForm",["clothStockIn.discount","clothStockIn.quantity"],["^[0-9]+.?[0-9]*$","^[1-9]+[0-9]*]*$"],["必须为数字","必须为整数");
 */
coos.form.validate.regexs = function(formId,fieldNames,regexs,messages)
{
	var message = message || "输入格式不对，请改正后提交！";
	var form = coos.$id(formId);	
	for(var i = 0; i < fieldNames.length; i++)
	{
		var oReg = new RegExp(regexs[i]);
		if(form[fieldNames[i]])
		{
			var value = form[fieldNames[i]].value.trim();
			if(value != "" && !oReg.test(value))
			{
				if(messages && messages[i])
				{
					alert(messages[i]);
				}
				else
				{
					alert(message);
				}
				form[fieldNames[i]].focus();
				return false;
			}
		}
	}
	return true;
};

coos.form.validate.data = function(data)
{
	var pass = false;
	if(data["unEmptyNames"].length > 0)
	{
		pass = coos.form.validate.emptys(data["formId"],data["unEmptyNames"],data["unEmptyMessages"]);
	}
	else
	{
		pass = true;
	}
	if(pass && data["regexNames"].length > 0)
	{
		pass = coos.form.validate.regexs(data["formId"],data["regexNames"],data["regexs"],data["regexMessages"]);
	}
	return pass;
};

/**
 * 通过传入json格式的数据,验证后提交
 * @example
 * function add()
	{
		var data ={
			formId          : "pageForm",
			unEmptyNames    : ["clothStockIn.lotNo","clothStockIn.quantity","clothStockIn.unitPrice"],
			unEmptyMessages : ["Lot No不能为空","存入数量不能为空","单价不能为空"],
			regexNames      : ["clothStockIn.quantity","clothStockIn.unitPrice","clothStockIn.discount"],
			regexs          : ["^[0-9]+.?[0-9]*$","^[0-9]+.?[0-9]*$","^[0-9]+.?[0-9]*$"],
			regexMessages   : ["存入数量必须为数字","单价必须为数字","折扣必须为数字"],
			end             : true
		};
		coos.form.validate.submit(data);
	}
 
 */
coos.form.validate.submit = function(data)
{
	var pass = false;
	pass = coos.form.validate.data(data);
	if(pass)
	{
		coos.$id(data["formId"]).submit();
		return false;
	}
};

/**
 * 光标移动到文本域清除默认提示，如果没有输入内容光标离开重新恢复默认提示。
 * onFocus="coos.form.clearDefaultText(this,'我来说两句')"，必须用onFocus再重新获得焦点时才起作用
 */
coos.form.clearDefaultText = function(el,message)
{
	var obj = coos.$obj(el);
	if(obj.value == message)
	{
		obj.value = "";
	}
	obj.onblur = function()
	{
		if(obj.value == "")
		{
			obj.value = message;
		}
	}
};
/**
 * window相关的函数
 */
coos.window = function(){};
coos.window.winWidth  = 0;
coos.window.winHeight = 0;

/**
 * 获取屏幕宽度的函数，在非xhtml标准页面下有可能有问题
 */
coos.window.width = function()
{
	if (window.innerWidth)//for Firefox
	{
		coos.window.winWidth = window.innerWidth;
	}
	else if((document.body) && (document.body.clientWidth))
	{
		coos.window.winWidth = document.body.clientWidth;
	}

	if (document.documentElement && document.documentElement.clientWidth)
	{
		coos.window.winWidth = document.documentElement.clientWidth;
	}
	return coos.window.winWidth;
};

/**
 * 获取屏幕高度的函数
 * html,body高度属性必须设值为height:100%否则在火狐浏览器下获取不到真实高度
 */
coos.window.height = function()
{
	if (window.innerHeight)//for Firefox
	{
		coos.window.winHeight = window.innerHeight;
	}
	else if((document.body) && (document.body.clientHeight))
	{
		coos.window.winHeight = document.body.clientHeight;
	}

	if (document.documentElement  && document.documentElement.clientHeight)
	{
		coos.window.winHeight = document.documentElement.clientHeight;
	}
	return coos.window.winHeight;
};

/**
 * 获取滚动条横向宽度
 */
coos.window.scrollWidth = function()
{
	return document.body.scrollWidth + "px";
};

/**
 * 获取滚动条竖向高度，取body.scrollHeight和documentElement.scrollHeight中最高的一个
 */
coos.window.scrollHeight = function()
{
	return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight) + "px";
};

coos.window.onscroll = function(){};

/**
 * window.onscroll绑定事件
 * @param fn 需要绑定的function
 */
coos.window.onscroll.add = function(fn)
{
	if (window.addEventListener) 
	{
		window.addEventListener("scroll",fn,false);
	}
	else
	{
		window.attachEvent("onscroll", fn);
	}
};

/**
 * 删除window.onscroll绑定事件
 * @param fn 需要绑定的function
 */
coos.window.onscroll.remove = function(fn)
{
	if (window.removeEventListener) 
	{
		window.addEventListener("scroll",fn,false);
	}
	else
	{
		window.detachEvent("onscroll", fn);
	}
};

/**
 * window.onload绑定事件
 * @param fn 需要绑定的function
 */
coos.window.onload = function(fn)
{
	if (window.addEventListener) 
	{
		window.addEventListener("load",fn,false);
	}
	else
	{
		window.attachEvent("onload", fn);
	}
};

/**
 * 让元素显示在屏幕中间,元素必须是绝对定位的
 * @param obj 要显示的对象,改变top left 属性值
 * @param event 触发的事件，在有滚动条的情况下必须传入事件以获取当时所在的滚动条高度
 * @example
<style type="text/css">
		html,body {margin: 0; padding: 0;height:100%;font-size: 14px;}
	  </style>
    <script type="text/javascript">  
	function show(event)
	{
		var obj = document.getElementById("showDiv");
		coos.window.center(obj,event);
		//元素在屏幕中间距离顶部的高度
		var top = coos.window.center.top(obj);
		//固顶在屏幕上,不随滚动条变化
		coos.window.fixed.set(obj,top);
		coos.window.fixed();
	}
    </script>
	<div id="showDiv" style="position:absolute;left:20px;top:5px;height:20px;width:400px;border:2px solid #ccc;text-align: center;clear: both;">
		I'm a div,I can show and fixed in center!
	</div>
	<div style="clear: both;margin:80px;height:1000px;">
		<br /><br />
		<a href="javascript:void(0)" onclick="show(event)">show div center</a>
	</div>
 */
coos.window.center = function(obj,event)
{
	var e = event || window.event;
	if(e)
	{
		obj.style.left = ((coos.window.width() - parseInt(obj.style.width,10))/2).toFixed() + "px";
		var objh = (parseInt(obj.style.height,10)/2).toFixed();
		var sh = parseInt(Math.max(document.body.scrollTop,document.documentElement.scrollTop),10);
		var wh = parseInt((coos.window.height()/2).toFixed(),10);
		var ch = sh + wh;
		obj.style.top  = (ch - objh) + "px";
	}
	else
	{
		obj.style.left = ((coos.window.width() - parseInt(obj.style.width,10))/2).toFixed() + "px";
		obj.style.top  = ((coos.window.height() - parseInt(obj.style.height,10))/2).toFixed() + "px";
	}
};

/**
 * 获取屏幕中间显示距离顶部的高度
 */
coos.window.center.top = function(obj)
{
	return ((coos.window.height() - parseInt(obj.style.height,10))/2).toFixed();
};

/**
 * 固顶元素在屏幕中显示，不随滚动条的变化而变化
 */
coos.window.fixed = function()
{
	coos.window.onscroll.add(coos.window.fixed.bind);
};

/**
 * 绑定需要固顶高度的元素window.onscroll事件
 */
coos.window.fixed.bind = function()
{
	if(!coos.window.fixed.obj || !coos.window.fixed.top)
	{
		return;
	}
	var objs = coos.window.fixed.obj;
	var tops = coos.window.fixed.top;
	var len = objs.length;
	//ie6.0以下不支持position:fixed;属性
	if(coos.browser.msie && parseInt(coos.browser.version) <= 6)
	{
		for(var i = 0; i < len;i++)
		{
			var sh = parseInt(Math.max(document.body.scrollTop,document.documentElement.scrollTop),10);
			objs[i].style.top = (sh + tops[i]) + "px";
		}
	}
	else
	{
		for(var i = 0; i < len;i++)
		{
			objs[i].style.position = "fixed";
			objs[i].style.top = tops[i] + "px";
		}
		//设置完position:fixed;属性和top属性后移除onscroll事件
		coos.window.onscroll.remove(coos.window.fixed.bind);
	}
};

/**
 * 设置需要固定高度的元素
 * @param obj 需要固定高度的元素对象
 * @param top 需要固定高度的元素距离顶部的高度
 */
coos.window.fixed.set = function(obj,top)
{
	if(!coos.window.fixed.obj)
	{
		coos.window.fixed.obj = new Array();
	}
	coos.window.fixed.obj.push(obj);
	
	if(!coos.window.fixed.top)
	{
		coos.window.fixed.top = new Array();
	}
	top = parseInt(top,10);
	coos.window.fixed.top.push(top);
};

